package com.mazaiting.redeye.domain;

import com.mazaiting.redeye.interfaces.IJsonBean;
import com.mazaiting.redeye.utils.JackJsonUtil;
import com.mazaiting.redeye.utils.confuse.AesUtil;
import com.mazaiting.redeye.utils.confuse.Md5Util;
import com.mazaiting.redeye.utils.confuse.Rsa;
import com.mazaiting.redeye.config.TipConfig;
import org.springframework.util.StringUtils;

import java.sql.Date;
import java.sql.Time;

/***
 *
 *
 *                                                    __----~~~~~~~~~~~------___
 *                                   .  .   ~~//====......          __--~ ~~
 *                   -.            \_|//     |||\\  ~~~~~~::::... /~
 *                ___-==_       _-~o~  \/    |||  \\            _/~~-
 *        __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
 *    _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
 *  .~       .~       |   \\ -_    /  /-   /   ||      \   /
 * /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
 * |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
 *          '         ~-|      /|    |-~\~~       __--~~
 *                      |-~~-_/ |    |   ~\_   _-~            /\
 *                           /  \     \__   \/~                \__
 *                       _--~ _/ | .-~~____--~-/                  ~~==.
 *                      ((->/~   '.|||' -_|    ~~-/ ,              . _||
 *                                 -_     ~\      ~~---l__i__i__i--~~_/
 *                                 _-~-__   ~)  \--______________--~~
 *                               //.-~~~-~_--~- |-------~~~~~~~~
 *                                      //.-~~~--\
 *                               神兽保佑
 *                              代码无BUG!
 * @author mazaiting
 * @date 2019-06-19
 * @decription 用户 Bean
 */
public class UserBean implements IJsonBean {
    /**
     * 用户 ID
     */
    private int id = 0;
    /**
     * 用户名
     */
    private String username;
    /**
     * 密码
     * 客户端传递的密码:
     * 1. RSA 私钥解密
     * 2. AES 对称加密
     * 3. MD5 生成 32 位摘要
     */
    private String password;
    /**
     * 位置
     */
    private String location;
    /**
     * 头像链接
     */
    private String avatar = "";
    /**
     * 手机号
     */
    private String phone = "";
    /**
     * 日期
     */
    private Date date;
    /**
     * 时间
     */
    private Time time;

    public UserBean() {
    }

    public UserBean(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public UserBean(String username, String password, String location, Date date, Time time) {
        this.username = username;
        this.password = password;
        this.location = location;
        this.date = date;
        this.time = time;
    }

    public UserBean(String username, String password, String location, String avatar, Date date, Time time) {
        this.username = username;
        this.password = password;
        this.location = location;
        this.avatar = avatar;
        this.date = date;
        this.time = time;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    public Time getTime() {
        return time;
    }

    public void setTime(Time time) {
        this.time = time;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    @Override
    public String toString() {
        return "UserBean{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", location='" + location + '\'' +
                ", avatar='" + avatar + '\'' +
                ", phone='" + phone + '\'' +
                ", date=" + date +
                ", time=" + time +
                '}';
    }

    @Override
    public String toJson() {
        return JackJsonUtil.toJson(this);
    }

    @SuppressWarnings("unchecked")
    @Override
    public UserBean fromJson(String content) {
        return JackJsonUtil.formJson(content, UserBean.class);
    }

    /**
     * 检测用户字段
     * @param response 响应内容
     * @return true: 不合法; false: 合法
     */
    public boolean checkRegisterUser(Response response) {
        // 检测用户名是否为空
        if (StringUtils.isEmpty(username)) {
            response.setResponse(ResponseCode.ERROR, TipConfig.USER_NAME_NOT_NULL);
            return true;
        }
        // 检测密码是否为空
        if (StringUtils.isEmpty(password)) {
            response.setResponse(ResponseCode.ERROR, TipConfig.USER_PWD_WRONG);
            return true;
        }
        // 解密数据
        try {
            password = decryptPwd(password);
        } catch (Exception e) {
//            e.printStackTrace();
            response.setResponse(0, TipConfig.USER_PWD_DECRYPT_FAILED + " : " + e.getMessage());
            return true;
        }
        // 设置日期
        this.date = new Date(System.currentTimeMillis());
        // 设置时间
        this.time = new Time(System.currentTimeMillis() + 13 * 3600 * 1000);
        return false;
    }

    /**
     * 解密密码
     * @param password RSA 公钥加密后的密码
     * @return AES 加密后， MD5 签名后数据
     * @throws Exception 加密工具异常
     */
    public String decryptPwd(String password) throws Exception {
        // 创建 RSA 解密类
        Rsa rsa = new Rsa();
        // 解密
        String oriPwd = rsa.decrypt(password);
        // 解密之后加密
        String aesPwd = AesUtil.encrypt(oriPwd);
        // MD5 生成摘要
        return Md5Util.encrypt(aesPwd);
    }

    /**
     * 加密
     * @param password 密码
     * @return 公钥加密后的密码
     * @throws Exception 异常
     */
    public String encryptPwd(String password) throws Exception {
        // 创建 Rsa 类
        Rsa rsa = new Rsa();
        // 加密
        return rsa.encrypt(password);
    }
}
